﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using SP_XHWeb.WebParts.GetShowList;
using Microsoft.SharePoint;

namespace SP_XHWeb.WebParts.GetShareList
{
    public partial class GetShareListUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetShowListItems();
            }
        }


        protected void GetShowListItems()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string SiteUrl = "/approveknowledge/default.aspx";
                string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                List<ShowDetails> itemList = GetShowListItemCollection("显示");
                if (itemList != null && itemList.Count != 0)
                {
                    using (SPSite site = new SPSite(hostUrl + SiteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists.TryGetList("ShowShareList");
                            if (list != null)
                            {
                                web.AllowUnsafeUpdates = true;

                                if (list.ItemCount > 0)
                                {
                                    foreach (ShowDetails sd in itemList)
                                    {
                                        //包含标示 0为不包含 1为包含
                                        int isContains = 0;
                                        foreach (SPListItem item in list.Items)
                                        {
                                            if (item.Title.Trim() == sd.Title.Trim() && item["Categories"].ToString().Trim() == (sd.WebName + "_" + sd.ListName).Trim() && item["SID"].ToString().Trim() == sd.SID.ToString().Trim())
                                            {
                                                isContains = 1;
                                            }

                                        }
                                        //不包含操作
                                        if (isContains == 0)
                                        {
                                            SPListItem newItem = list.Items.Add();
                                            newItem["分类"] = sd.Categories;
                                            newItem["Categories"] = sd.WebName + "_" + sd.ListName;
                                            try
                                            {
                                                newItem["标题"] = sd.Title;
                                            }
                                            catch
                                            {
                                                newItem["Title"] = sd.Title;
                                            }
                                            newItem["Content"] = "@" + sd.Content + "@";
                                            newItem["SID"] = sd.SID;
                                            newItem["创建时间"] = DateTime.Parse(sd.CreateTime);
                                            newItem.Update();
                                        }
                                    }
                                }
                                else
                                {
                                    foreach (ShowDetails iitem in itemList)
                                    {
                                        SPListItem item = list.Items.Add();
                                        try
                                        {
                                            item["标题"] = iitem.Title;
                                        }
                                        catch
                                        {
                                            item["Title"] = iitem.Title;
                                        }
                                        item["Content"] = "@" + iitem.Content + "@";
                                        item["分类"] = iitem.Categories;
                                        item["SID"] = iitem.SID.ToString();
                                        item["Categories"] = iitem.WebName + "_" + iitem.ListName;
                                        item["创建时间"] = DateTime.Parse(iitem.CreateTime);
                                        item.Update();
                                    }
                                }

                            }
                        }
                    }
                }
            });
        }


        /// <summary>
        /// 获取所有子站点下首页显示的ListItem
        /// </summary>
        protected List<ShowDetails> GetShowListItemCollection(string isShow)
        {
            List<ShowDetails> itemList = new List<ShowDetails>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string SiteUrl = "/default.aspx";
                string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;

                using (SPSite site = new SPSite(hostUrl + SiteUrl))
                {
                    using (SPWeb pweb = site.OpenWeb())
                    {
                        //子站点
                        if (pweb.Webs.Count != 0)
                        {
                            foreach (SPWeb web in pweb.Webs)
                            {
                                //List集合
                                foreach (SPList list in web.Lists)
                                {
                                    if (list.Fields.ContainsField("首页显示") && list.ItemCount != 0)
                                    {
                                        //ListItem集合
                                        foreach (SPListItem item in list.Items)
                                        {
                                            if (item["首页显示"] != null && !string.IsNullOrEmpty(item["首页显示"].ToString()) && item["首页显示"].ToString() == isShow)
                                            {
                                                if (item.ParentList.Title.Contains("KnowledgeShare"))
                                                {
                                                    ShowDetails sd = new ShowDetails();
                                                    sd.SID = item.ID;
                                                    sd.WebName = item.ParentList.ParentWeb.Name;
                                                    sd.ListName = item.ParentList.Title;
                                                    sd.Title = item.Name;
                                                    sd.Content = item.Url;
                                                    sd.Categories = item.ParentList.ParentWeb.Title;
                                                    sd.Categories += "_知识中心";
                                                    sd.CreateTime = item["创建时间"].ToString();
                                                    itemList.Add(sd);
                                                }

                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            });
            return itemList;
        }
    }
}
